'use strict';

// 获取Controller类
const Controller = require('egg').Controller;

class EmailController extends Controller {
  async sendVerifyCode() {
    const { ctx } = this;

    // 获取 email 邮箱地址
    const { email } = ctx.request.body;

    // 发送验证码
    const success = await ctx.service.email.sendVerifyCode(email);

    ctx.body = ctx.helper.formatResponse({ success }, 200, '验证码发送成功');
  }

  async checkVerifyCode() {
    const { ctx } = this;

    // 获取 email 和 验证码，校验验证码是否有效
    const { email, verifyCode } = ctx.request.body;

    // 获取结果
    const success = await ctx.service.email.checkVerifyCode(email, verifyCode);

    ctx.body = ctx.helper.formatResponse({ success }, 200, '校验成功，验证码正确');
  }
}

module.exports = EmailController;
